package leetcode.hot100;

import java.util.Arrays;
import java.util.HashSet;
import java.util.Set;

class LongestConsecutive {
    public static void main(String[] args) {
        int[] nums = {1,2,0,1};
        int i = new LongestConsecutive().longestConsecutive(nums);
        System.out.println(i);
    }

    public int longestConsecutive(int[] nums) {
        int len = nums.length;
        if (len == 0) {
            return 0;
        } else {
            int longestStreak = 1;
            Arrays.sort(nums);

            for (int i = 0; i < len; i++) {
                int currValue = 1;
                while (i < len - 1 && (nums[i] == nums[i + 1] || nums[i] + 1 == nums[i + 1])) {
                    if (nums[i] + 1 == nums[i + 1]){
                        currValue += 1;
                    }
                    i++;
                }
                longestStreak = Math.max(currValue, longestStreak);
            }
            return longestStreak;
        }
    }
}
